﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using FMSpaceShooter.Screens;
using FMSpaceShooter.GameObjects;
using System.Reflection;

namespace FMSpaceShooter.GameData
{
    static class Factories
    {
        static Dictionary<string, Type> enemyTypes;

        static Dictionary<string, Type> bulletTypes;

        public static Dictionary<string, Type> BulletTypes { get { return bulletTypes; } }
        
        static Factories() {

            var types = Assembly.GetCallingAssembly().GetTypes();

            enemyTypes = types
            .Where(t => t.BaseType == typeof(EnemyBase))
            .ToDictionary(t => (string)t.GetField("TypeName").GetValue(null), t => t);

            var selectedTypes = types.Where(t => 
                t.BaseType == typeof(BulletBase) && t.GetField("IsStandardBullet") != null && (bool)t.GetField("IsStandardBullet").GetValue(null)
                );

            bulletTypes = selectedTypes.ToDictionary(t => (string)t.GetField("TypeName").GetValue(null), t => t);
        }

        public static EnemyBase BadGuyFactory(GameplayScreen screen, string type, Vector2 position)
        {
            if (enemyTypes.ContainsKey(type))
            {
                return (EnemyBase)Activator.CreateInstance(enemyTypes[type], screen, position);
            }
            throw new Exception("Enemy Not Found");
            
            //return null;
        }

        public static BulletBase BulletFactory(GameplayScreen screen, string type, Vector2 position, float rotation)
        {
            if (bulletTypes.ContainsKey(type))
            {
                return (BulletBase)Activator.CreateInstance(bulletTypes[type], screen, position, rotation);
            }
            throw new Exception("Bullet Not Found");
        }
    }
}
